package com.qiaofang.housewarehouse.stub.constants;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public enum HouseTypeEnum {
    ONE("一室", 1),
    TWO("二室", 2),
    THREE("三室", 3),
    FOUR("四室", 4),
    FIVE("五室及以上", 5);
    /**
     * 枚举值
     */
    private int value;
    /**
     * 描述
     */
    private String desc;

    HouseTypeEnum(String desc, int value) {
        this.value = value;
        this.desc = desc;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public static HouseTypeEnum getEnum(int value) {
        HouseTypeEnum resultEnum = null;
        HouseTypeEnum[] enumAry = HouseTypeEnum.values();
        for (int i = 0; i < enumAry.length; i++) {
            if (enumAry[i].getValue() == value) {
                resultEnum = enumAry[i];
                break;
            }
        }
        return resultEnum;
    }

    public static Map<String, Map<String, Object>> toMapMap() {
        HouseTypeEnum[] ary = HouseTypeEnum.values();
        Map<String, Map<String, Object>> enumMap = new HashMap<String, Map<String, Object>>();
        for (int num = 0; num < ary.length; num++) {
            Map<String, Object> map = new HashMap<String, Object>();
            String key = String.valueOf(getEnum(ary[num].getValue()));
            map.put("value", String.valueOf(ary[num].getValue()));
            map.put("desc", ary[num].getDesc());
            enumMap.put(key, map);
        }
        return enumMap;
    }

    public static Map<String, Integer> toMap() {
        HouseTypeEnum[] ary = HouseTypeEnum.values();
        Map<String,Integer> enumMap = new HashMap<>();
        for (int i = 0; i < ary.length; i++) {
            enumMap.put(ary[i].getDesc(), ary[i].getValue());
        }
        return enumMap;
    }

    public static List toList() {
        HouseTypeEnum[] ary = HouseTypeEnum.values();
        List list = new ArrayList();
        for (int i = 0; i < ary.length; i++) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("value", String.valueOf(ary[i].getValue()));
            map.put("desc", ary[i].getDesc());
            list.add(map);
        }
        return list;
    }

    /**
     * 取枚举的json字符串
     *
     * @return
     */
    public static String getJsonStr() {
        HouseTypeEnum[] enums = HouseTypeEnum.values();
        StringBuffer jsonStr = new StringBuffer("[");
        for (HouseTypeEnum senum : enums) {
            if (!"[".equals(jsonStr.toString())) {
                jsonStr.append(",");
            }
            jsonStr.append("{id:'").append(senum.toString()).append("',desc:'").append(senum.getDesc()).append("',value:'").append(senum.getValue()).append("'}");
        }
        jsonStr.append("]");
        return jsonStr.toString();
    }

    public static void main(String[] args) {
        Map<String, Integer> stringIntegerMap = HouseTypeEnum.toMap();
        for (String key : stringIntegerMap.keySet() ) {
            System.out.println("key:" + key + ",value:" + stringIntegerMap.get(key));
        }
    }
}
 